package com.onlyphoto.onlymall.mvp.view.binder

import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition

import com.onlyphoto.onlymall.R
import com.bumptech.glide.Glide
import com.onlyphoto.onlymall.utils.ConvertUtil

import me.drakeet.multitype.ItemViewBinder

/**
 * Created by gaochujia on 2019-07-09.
 */
class DetailsImagesBinder : ItemViewBinder<String, DetailsImagesBinder.ViewHolder>() {

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.item_image, parent, false)
        return ViewHolder(root)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, url: String) {
        viewHolder.update(url)

    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var imgDetails: ImageView? = null

        init {
            imgDetails = itemView.findViewById(R.id.img_details)
        }

        fun update(url: String) {
            Glide.with(imgDetails!!.context).asDrawable().load(url).into(object : CustomTarget<Drawable>(){
                override fun onLoadCleared(placeholder: Drawable?) {
                }

                override fun onResourceReady(drawable: Drawable, transition: Transition<in Drawable>?) {
                    val dpH = drawable.intrinsicHeight / 2
                    val dpW = drawable.intrinsicWidth / 2
                    val params = LinearLayout.LayoutParams(ConvertUtil.dp2px(imgDetails!!.context, dpW.toFloat()), ConvertUtil.dp2px(imgDetails!!.context, dpH.toFloat()))
                    imgDetails?.layoutParams = params
                    imgDetails?.setImageDrawable(drawable)
                }
            })
        }

    }


}
